<template>
  <div class="login">
    <!-- 登录表单 -->
    <el-form
      :model="loginForm"
      status-icon
      :rules="rules"
      ref="loginForm"
      label-width="100px"
      class="demo-loginForm"
    >
      <h1 class="title">
        <i class="el-icon-loading"></i>Vuexms登录
      </h1>
      <el-form-item label="帐号 :" prop="username">
        <el-input type="text" v-model="loginForm.username" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码 :" prop="password">
        <el-input type="password" v-model="loginForm.password" autocomplete="off"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('loginForm')">登录</el-button>
        <el-button @click="resetForm('loginForm')">清空</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      //登录表单的数据
      loginForm: {
        username: "",
        password: ""
      },
      //验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 12, message: "长度在 3 到 12 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 18, message: "长度在 6 到 18 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.loginForm.username, this.loginForm.password);
          //保留this
          var _this = this;
          //收集用户名和密码，发送给后台
          this.axios
            .post("/api/checklogin", {
              username: _this.loginForm.username,
              password: _this.loginForm.password
            })
            .then(result => {
              console.log("接收后端响应登录的数据", result.data);
              if (result.data.length) {
                //把当前登录用户信息存入state
                this.$store.commit("save_userinfo", result.data[0]);
                this.$message({
                  showClose: true,
                  message: "恭喜你，登录成功！",
                  type: "success"
                });
                this.$router.push("/home");
              } else {
                this.$message({
                  showClose: true,
                  message: "登录失败，请确认用户名和密码后再登录！",
                  type: "error"
                });
              }
            })
            .catch(err => {
              console.log(err);
            });
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
.login {
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}
.el-form {
  width: 400px;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding: 50px 50px 10px 10px;
}
.el-form .title {
  font-size: 22px;
  color: #333;
  font-weight: bold;
  padding: 20px 0 20px 60px;
}
.el-form .title i {
  margin-right: 15px;
}
</style>
